package com.blkj.iam;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

/**
 * 身份认证与授权系统
 *
 * @author Ethan
 */
@Slf4j
@SpringBootApplication
@EnableScheduling
public class IamApplication {

    public static void main(String[] args) {
        SpringApplication.run(IamApplication.class, args);
    }

    @Component
    public static class ServerStartupRunner implements ApplicationListener<ServletWebServerInitializedEvent> {

        @Override
        public void onApplicationEvent(ServletWebServerInitializedEvent event) {
            int port = event.getWebServer().getPort();
            String host = "localhost";
            String contextPath = event.getApplicationContext().getEnvironment().getProperty("server.servlet.context-path", "");
            String scheme = "http";

            // 检查是否启用了 SSL
            String serverSslEnabled = event.getApplicationContext().getEnvironment().getProperty("server.ssl.enabled", "false");
            if ("true".equalsIgnoreCase(serverSslEnabled)) {
                scheme = "https";
            }

            // 打印访问路径
            log.info("IAM-Server 启动成功 ......");
            log.info("访问路径: {}://{}:{}{}", scheme, host, port, contextPath);
        }
    }
}
